import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter_alibc/flutter_alibc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/pages/user/order/jyz_order_page.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_yres/pages/vip/vip_shop_page.dart';
import 'package:flutter_yres/pages/user/InformNews.dart';
import 'package:flutter_yres/pages/user/SystemSet.dart';
import 'package:flutter_yres/pages/user/address/MyAddress.dart';
import 'package:flutter_yres/pages/user/vip_order/OrderPage.dart';
import 'package:flutter_yres/pages/user/OrderPage.dart' as t;

import 'package:flutter_yres/pages/user/Form.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_yres/pages/user/DrawMoney.dart';
import 'package:flutter_yres/pages/user/BindZfb.dart';
import 'package:flutter_yres/pages/user/InviteFriend.dart';
// import '../pages/user/InviteFriendcopy.dart';
import 'package:flutter_yres/tool/application.dart';
import 'package:flutter_yres/pages/event/LoginEvent.dart';
import 'package:flutter_yres/tool/AppSaveInfoUtil.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../pages/user/balanceList.dart';
import '../pages/user/team/Team.dart';
import '../pages/user/ArticleDetail.dart';
import 'package:flutter_yres/tool/LoadingUtil.dart';
import '../pages/user/WebPage.dart';

class MinePage extends StatefulWidget {
  MinePage({Key key}) : super(key: key);

  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with WidgetsBindingObserver {
  EasyRefreshController _controller = new EasyRefreshController();
  ScrollController scrollController = ScrollController();
  int _currentState = 0; //0 加油站  1充电站

  String token = '';
  List<Map> userTools = [
    {'name': '懂车帝', 'icon': 'images/car_dcd.png'},
    {'name': '58', 'icon': 'images/car_58.png'},
    {'name': '购物订单', 'icon': 'images/icon_navlist_a.png'},
    // {'name': '发票', 'icon': 'images/icon_navlist_b.png'},
    {'name': '邀请好友', 'icon': 'images/icon_navlist_d.png'},

    {'name': '区域代理', 'icon': 'images/icon_navlist_d.png'},
    {'name': '产品业绩', 'icon': 'images/icon_navlist_e.png'},
    {'name': '反馈', 'icon': 'images/icon_navlist_f.png'},
    {'name': '客服', 'icon': 'images/icon_navlist_g.png'},
    {'name': '关于', 'icon': 'images/icon_navlist_h.png'},
    {'name': '地址管理', 'icon': 'images/tool_e.png'},
    {'name': '会员订单', 'icon': 'images/tool_b.png'},
    {'name': '合作', 'icon': 'images/icon_navlist_c.png'},
  ];

  Map user_msg = {};
  Map user_detail = {};
  String avatar;

  String balance = '0'; //	余额
  String point = '0'; //	积分
  String amount_economize = '0'; //	共节省多少元
  String amount_today = '0'; //	今日收益
  String amount_current = '0'; //	本月预估收入
  String amount_last = '0'; //	上月结算

  String uid = '';

  // double bannerHeight = ScreenUtil.getInstance().setWidth(750) * 0.4;
  // double itemHeight = ScreenUtil.getInstance().setHeight(330);
  // double imgItemHeight = ScreenUtil.getInstance().setHeight(210);
  // double guanggaoHeight = ScreenUtil.getInstance().setWidth(750) * 0.24;
  // double tabHeight = ScreenUtil.getInstance().setWidth(80);
  // double _listHeight = ScreenUtil.getInstance().setHeight(210); //单个列表高度
  // double kScreenWidth = ScreenUtil.getInstance().setWidth(750);
  // double kScreenHeight = ScreenUtil.getInstance().setHeight(1334);
  // double kMargin5 = ScreenUtil.getInstance().setWidth(5);
  // double kMargin10 = ScreenUtil.getInstance().setWidth(10);
  // double kMargin8 = ScreenUtil.getInstance().setWidth(8);
  // double kMargin15 = ScreenUtil.getInstance().setWidth(15);
  // double kMargin20 = ScreenUtil.getInstance().setWidth(20);
  // double kMargin25 = ScreenUtil.getInstance().setWidth(25);
  // double kMargin28 = ScreenUtil.getInstance().setWidth(28);
  // double kMargin100 = ScreenUtil.getInstance().setWidth(100);
  // double kMargin120 = ScreenUtil.getInstance().setWidth(120);
  double kWindowstateHeight =
      MediaQueryData.fromWindow(window).padding.top; //状态栏

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Scaffold(
        body: new EasyRefresh(
            header: MaterialHeader(),
            controller: _controller,
            onRefresh: () {
              _getLoginMsg();
              // getUserInfo();
              // getTuiguang();
            },
            child: SingleChildScrollView(
                child: Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: _currentState == 0
                            ? AssetImage("images/bg_top_personal_oil.png")
                            : AssetImage('images/bg_top_personal_charge.png'),
                        fit: BoxFit.cover,
                      ),
                    ),
                    child: Column(
                      children: <Widget>[
                        Container(
                            margin: EdgeInsets.fromLTRB(
                                0, kWindowstateHeight, 0, 0),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  flex: 4,
                                  child: Container(),
                                ),
                                Expanded(
                                    flex: 1,
                                    child: GestureDetector(
                                      child: Text(
                                        '设置',
                                        style: TextStyle(color: white,fontSize: 16),
                                      ),
                                      onTap: () {
                                        Navigator.of(context).push(
                                            new MaterialPageRoute(
                                                builder: (context) {
                                          return new SystemPage();
                                        }));
                                      },
                                    )
                                    // IconButton(
                                    //   iconSize: kScreenWidth * 0.1,
                                    //   color: Colors.white,
                                    //   icon: Image.asset(
                                    //       'images/user_user_icon_nav_setup.png'),
                                    //   onPressed: () {
                                    //     Navigator.of(context).push(
                                    //         new MaterialPageRoute(
                                    //             builder: (context) {
                                    //       return new SystemPage();
                                    //     }));
                                    //   },
                                    // ),
                                    ),
                                Expanded(
                                  flex: 1,
                                  child: IconButton(
                                    iconSize:
                                        ScreenUtil.getInstance().setWidth(750) *
                                            0.1,
                                    color: Colors.white,
                                    icon: Image.asset(
                                        'images/user_user_icon_nav_messages.png'),
                                    onPressed: () {
                                      Navigator.of(context).push(
                                          new MaterialPageRoute(
                                              builder: (context) {
                                        return new InformNewsPage();
                                      }));
                                    },
                                  ),
                                )
                              ],
                            )),
                        Container(
                          child: Row(
                            children: <Widget>[
                              Container(
                                  margin: EdgeInsets.all(
                                      ScreenUtil.getInstance().setWidth(10)),
                                  child: ClipOval(
                                      child: avatar == null || avatar == ""
                                          ? Image(
                                              image: AssetImage(
                                                  'images/user_icon_user.png'),
                                              width: ScreenUtil.getInstance()
                                                      .setWidth(750) *
                                                  0.18,
                                              height: ScreenUtil.getInstance()
                                                      .setWidth(750) *
                                                  0.18,
                                              fit: BoxFit.cover,
                                            )
                                          : avatar.startsWith("http")
                                              ? CachedNetworkImage(
                                                  width:
                                                      ScreenUtil.getInstance()
                                                              .setWidth(750) *
                                                          0.18,
                                                  height:
                                                      ScreenUtil.getInstance()
                                                              .setWidth(750) *
                                                          0.18,
                                                  fit: BoxFit.cover,
                                                  imageUrl: avatar,
                                                  placeholder: (context, url) =>
                                                      Image.asset(
                                                    'images/pic_default.png',
                                                    width:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    height:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    fit: BoxFit.cover,
                                                  ),
                                                  errorWidget:
                                                      (context, url, error) =>
                                                          Image.asset(
                                                    'images/pic_default.png',
                                                    width:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    height:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    fit: BoxFit.cover,
                                                  ),
                                                )
                                              : CachedNetworkImage(
                                                  width:
                                                      ScreenUtil.getInstance()
                                                              .setWidth(750) *
                                                          0.18,
                                                  height:
                                                      ScreenUtil.getInstance()
                                                              .setWidth(750) *
                                                          0.18,
                                                  fit: BoxFit.cover,
                                                  imageUrl:
                                                      Api.ImgBaseUrl + avatar,
                                                  placeholder: (context, url) =>
                                                      Image.asset(
                                                    'images/pic_default.png',
                                                    width:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    height:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    fit: BoxFit.cover,
                                                  ),
                                                  errorWidget:
                                                      (context, url, error) =>
                                                          Image.asset(
                                                    'images/pic_default.png',
                                                    width:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    height:
                                                        ScreenUtil.getInstance()
                                                                .setWidth(750) *
                                                            0.18,
                                                    fit: BoxFit.cover,
                                                  ),
                                                ))),
                              Expanded(
                                flex: 2,
                                child: Column(
                                  children: <Widget>[
                                    Container(
                                      alignment: Alignment.topLeft,
                                      padding: EdgeInsets.fromLTRB(
                                          ScreenUtil.getInstance().setWidth(20),
                                          0,
                                          0,
                                          0),
                                      child: RichText(
                                          text: TextSpan(
                                              style:
                                                  TextStyle(color: Colors.grey),
                                              children: [
                                            TextSpan(
                                                text: user_msg['phone'] == null
                                                    ? ''
                                                    : user_msg['phone'] + ' ',
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(30),
                                                    fontWeight:
                                                        FontWeight.w600)),
                                            TextSpan(
                                                text: user_msg['group_id'] ==
                                                        '1'
                                                    ? ''
                                                    : (user_msg['group_id'] ==
                                                            '2'
                                                        ? '油掌柜'
                                                        : (user_msg['group_id'] ==
                                                                '3'
                                                            ? '油站长'
                                                            : '服务商')),
                                                style: TextStyle(
                                                    color: Colors.orange,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(30)))
                                          ])),
                                      //  Text(
                                      //     user_msg['phone'] == null
                                      //         ? ''
                                      //         : user_msg['phone'],
                                      //     style: TextStyle(
                                      //         color: Colors.white,
                                      //         fontSize: 19,
                                      //         fontWeight: FontWeight.w600),
                                      //     textAlign: TextAlign.left)
                                    ),
                                    GestureDetector(
                                      child: Container(
                                          alignment: Alignment.bottomLeft,
                                          padding: EdgeInsets.fromLTRB(
                                              ScreenUtil.getInstance()
                                                  .setWidth(20),
                                              0,
                                              0,
                                              0),
                                          child: user_msg['group_id'] == '4'
                                              ? Text('')
                                              : Container(
                                                  padding: EdgeInsets.fromLTRB(
                                                      8, 2, 8, 2),
                                                  decoration: BoxDecoration(
                                                    color: Colors.orange, // ��色
                                                    //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            20.0), // 也可控件一边
                                                  ),
                                                  // color: Colors.yellow[300],
                                                  child: Text(
                                                    '购买商品',
                                                    style: TextStyle(
                                                        color: Colors.white,
                                                        fontSize: 13),
                                                  ),
                                                )),
                                      onTap: () {
                                        if (token == null || token == '') {
                                          //未登陆
                                          Navigator.of(context)
                                              .pushNamed("login");
                                        } else {
                                          showDialog(
                                              context: context,
                                              barrierDismissible: false,
                                              builder: (_) {
                                                return new NetLoadingDialog(
                                                  loadingText: "跳转中...",
                                                  bgColor: Color.fromARGB(
                                                      255, 0, 0, 0),
                                                  requestCallBack: null,
                                                  outsideDismiss: false,
                                                );
                                              });
                                          goToBuyVip();
                                        }
                                      },
                                    )
                                  ],
                                ),
                              ),
                              // Expanded(
                              //     flex: 1,
                              //     child: Container(
                              //         child: GestureDetector(
                              //       child: Image(
                              //         image:
                              //             AssetImage('images/icon_cashout.png'),
                              //       ),
                              //       onTap: () {
                              //         //
                              //         card();
                              //       },
                              //     )))
                              // Expanded(
                              //     flex: 1,
                              //     child: Container(
                              //         child: GestureDetector(
                              //       child: Image(
                              //         image: _currentState == 0
                              //             ? AssetImage(
                              //                 'images/user_icon_btn_charge.png')
                              //             : AssetImage(
                              //                 'images/user_icon_btn_oil.png'),
                              //       ),
                              //       onTap: () {
                              //         //切换模式
                              //         if (_currentState == 0) {
                              //           setState(() {
                              //             _currentState = 1;
                              //           });
                              //         } else {
                              //           setState(() {
                              //             _currentState = 0;
                              //           });
                              //         }
                              //       },
                              //     )))
                            ],
                          ),
                        ),
                        Container(
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                  flex: 1,
                                  child: GestureDetector(
                                    onTap: () {
                                      //
                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              //导航打开新视图
                                              builder: (context) =>
                                                  BalanceListPage() //打开第二个页面，并传值，传递值的名称是model
                                              ));
                                    },
                                    child: Column(
                                      children: <Widget>[
                                        Container(
                                            child: Text(
                                          balance,
                                          style: TextStyle(
                                              fontSize: 25,
                                              color: Colors.white),
                                        )),
                                        Container(
                                            child: Text(
                                          _currentState == 0 ? '余额' : '充电余额',
                                          style: TextStyle(
                                              fontSize: 15,
                                              color: Colors.white),
                                        )),
                                      ],
                                    ),
                                  )),
                              Expanded(
                                flex: 1,
                                child: Column(
                                  children: <Widget>[
                                    Container(
                                        child: Text(
                                      point,
                                      style: TextStyle(
                                          fontSize: 25, color: Colors.white),
                                    )),
                                    Container(
                                        child: Text(
                                      '积分',
                                      style: TextStyle(
                                          fontSize: 15, color: Colors.white),
                                    )),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                        Container(
                          decoration: BoxDecoration(
                            color: white,
                            borderRadius: BorderRadius.circular(8.0),
                          ),
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setWidth(20),
                              ScreenUtil.getInstance().setWidth(20),
                              ScreenUtil.getInstance().setWidth(20),
                              0),
                          child: Column(
                            children: <Widget>[
                              Container(
                                // color: black,
                                decoration: BoxDecoration(
                                  color: black,
                                  borderRadius: BorderRadius.circular(8.0),
                                ),
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                        child: Container(
                                      margin: EdgeInsets.all(
                                          ScreenUtil.getInstance()
                                              .setWidth(20)),
                                      child: Text(
                                        user_msg['balance'] == null
                                            ? '余额 ¥0'
                                            : '余额 ¥' + balance,
                                        style: TextStyle(
                                            color: Color.fromRGBO(
                                                230, 202, 185, 1),
                                            fontSize: ScreenUtil.getInstance()
                                                .setSp(30)),
                                      ),
                                    )),
                                    GestureDetector(
                                        onTap: () {
                                          card();
                                        },
                                        child: Container(
                                          decoration: BoxDecoration(
                                            color: Color.fromRGBO(
                                                230, 202, 185, 1),
                                            borderRadius:
                                                BorderRadius.circular(8.0),
                                          ),
                                          alignment: Alignment.center,
                                          margin: EdgeInsets.all(
                                              ScreenUtil.getInstance()
                                                  .setWidth(20)),
                                          width: ScreenUtil.getInstance()
                                              .setWidth(120),
                                          height: ScreenUtil.getInstance()
                                              .setWidth(56),
                                          child: Text(
                                            '立即提现',
                                            style: TextStyle(
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(24)),
                                          ),
                                        ))
                                  ],
                                ),
                              ),
                              Container(height: 10),
                              Row(
                                children: <Widget>[
                                  Expanded(
                                      child: Container(
                                    child: Column(
                                      children: <Widget>[
                                        Text(
                                          '¥' + amount_current,
                                          style: TextStyle(
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(40)),
                                        ),
                                        Container(height: 5),
                                        Text('本月预估',
                                            style: TextStyle(
                                                color: Colors.grey,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(24))),
                                        Container(height: 5),
                                        RichText(
                                          textAlign: TextAlign.center,
                                          text: TextSpan(
                                              style:
                                                  TextStyle(color: Colors.grey),
                                              children: [
                                                TextSpan(
                                                    text: '上月结算',
                                                    style: TextStyle(
                                                        fontSize: ScreenUtil
                                                                .getInstance()
                                                            .setSp(24))),
                                                TextSpan(
                                                    text: '¥' + amount_last,
                                                    style: TextStyle(
                                                        fontSize: ScreenUtil
                                                                .getInstance()
                                                            .setSp(24),
                                                        color: Colors.orange)),
                                              ]),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                        Container(height: 15),
                                      ],
                                    ),
                                  )),
                                  Expanded(
                                      child: Container(
                                    child: Column(
                                      children: <Widget>[
                                        Text(
                                          '¥' + amount_today,
                                          style: TextStyle(
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(40)),
                                        ),
                                        Container(height: 5),
                                        Text('今日收益',
                                            style: TextStyle(
                                                color: Colors.grey,
                                                fontSize:
                                                    ScreenUtil.getInstance()
                                                        .setSp(24))),
                                        Container(height: 5),
                                        RichText(
                                          textAlign: TextAlign.center,
                                          text: TextSpan(
                                              style:
                                                  TextStyle(color: Colors.grey),
                                              children: [
                                                TextSpan(
                                                    text: '共节省',
                                                    style: TextStyle(
                                                        fontSize: ScreenUtil
                                                                .getInstance()
                                                            .setSp(24))),
                                                TextSpan(
                                                    text:
                                                        '¥' + amount_economize,
                                                    style: TextStyle(
                                                        fontSize: ScreenUtil
                                                                .getInstance()
                                                            .setSp(24),
                                                        color: Colors.orange)),
                                              ]),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                        Container(height: 15),
                                      ],
                                    ),
                                  ))
                                ],
                              )
                            ],
                          ),
                        ),
                        Container(
                          child: _currentState == 0
                              ? _buildChangeJYZ()
                              : _buildChangeCDZ(),
                        )
                      ],
                    )))));
  }

  Widget _itemBuilder(BuildContext context, item) {
    return InkWell(
      onTap: () async {
        if (item['name'] == '懂车帝') {
          Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
            return new WebPage(
                url: "https://m.dcdapp.com/motor/m/", name: '懂车帝');
          }));
          return ;
        }
        if (item['name'] == '58') {
          Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
            return new WebPage(
                url:
                    "https://m.58.com/nj/ershouche/?reform=pcfront&listfrom=dspadvert&spm=124093495970.sem_esc_baicaipinpai__dtmax_20181029&utm_source=sem-esc-baidu-m",
                name: '58');
          }));
          return ;
        }
        if (item['name'] == '购物订单') {
           Navigator.push(
              context,
              MaterialPageRoute(
                  //导航打开新视图
                  builder: (context) =>
                      t.OrderPage(index: 0,) //打开第二个页面，并传值，传递值的名称是model
                  ));
          return ;
        }
        if (item['name'] == '地址管理') {
          Navigator.push(
              context,
              MaterialPageRoute(
                  //导航打开新视图
                  builder: (context) =>
                      MyAddressPage() //打开第二个页面，并传值，传递值的名称是model
                  ));
                  return ;
        }
        if (item['name'] == '会员订单') {
          Navigator.push(
              context,
              MaterialPageRoute(
                  //导航打开新视图
                  builder: (context) => OrderPage() //打开第二个页面，并传值，传递值的名称是model
                  ));
                  return ;
        }
        if (item['name'] == '合作') {
          Navigator.of(context)
              .push<String>(MaterialPageRoute(builder: (context) {
            return new FormPage();
          }));
          return ;
        }
        if (item['name'] == '区域代理') {
          Navigator.of(context)
              .push<String>(MaterialPageRoute(builder: (context) {
            return new FormPage(type: 1);
          }));
          return ;
        }
        if (item['name'] == '反馈') {
          Navigator.of(context)
              .push<String>(MaterialPageRoute(builder: (context) {
            return new FormPage(type: 3);
          }));
          return ;
        }
        if (item['name'] == '产品业绩') {
          Navigator.of(context)
              .push<String>(MaterialPageRoute(builder: (context) {
            return new TeamPage();
          }));
          return ;
        }
        if (item['name'] == '邀请好友') {
          Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
            return new InviteFriendPage(uid: uid);
          }));
          return ;
        }
        if (item['name'] == '关于') {
          Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
            return new ArticleDetailPage(
              article_id: '3',
            );
          }));
          return ;
        }
        if (item['name'] == '客服') {
          Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
            return new ArticleDetailPage(
              article_id: '7',
            );
          }));
          return ;
        }
      },
      child: Column(
        children: <Widget>[
          Container(
            height: ScreenUtil.getInstance().setWidth(20),
          ),
          Image(
            image: AssetImage(item['icon']),
            width: ScreenUtil.getInstance().setWidth(90),
            height: ScreenUtil.getInstance().setWidth(90),
          ),
          Text(item['name'])
        ],
      ),
    );
  }

//加油站
  _buildChangeJYZ() {
    return Container(
        margin: EdgeInsets.fromLTRB(
            ScreenUtil.getInstance().setWidth(15),
            ScreenUtil.getInstance().setWidth(20),
            ScreenUtil.getInstance().setWidth(15),
            0),
        child: Column(
          children: <Widget>[
            Card(
                elevation: ScreenUtil.getInstance().setWidth(10),
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          flex: 5,
                          child: Container(
                            margin: EdgeInsets.fromLTRB(
                                ScreenUtil.getInstance().setWidth(20),
                                ScreenUtil.getInstance().setWidth(28),
                                ScreenUtil.getInstance().setWidth(10),
                                ScreenUtil.getInstance().setWidth(10)),
                            child: Text(
                              '加油订单',
                              style: TextStyle(fontSize: 16),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: Icon(Icons.next_week),
                        ),
                      ],
                    ),
                    Container(
                        margin: EdgeInsets.fromLTRB(
                            0,
                            ScreenUtil.getInstance().setWidth(28),
                            0,
                            ScreenUtil.getInstance().setWidth(28)),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 1,
                                child: GestureDetector(
                                  child: Column(
                                    children: <Widget>[
                                      Image(
                                        image: AssetImage(
                                            'images/user_icon_order_all.png'),
                                        width: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                        height: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                      ),
                                      Text('全部')
                                    ],
                                  ),
                                  onTap: () {
                                    Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            //导航打开新视图
                                            builder: (context) =>
                                                JYZOrderPage() //打开第二个页面
                                            ));
                                  },
                                )),
                            Expanded(
                                flex: 1,
                                child: GestureDetector(
                                  child: Column(
                                    children: <Widget>[
                                      Image(
                                        image: AssetImage(
                                            'images/user_icon_order_paid.png'),
                                        width: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                        height: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                      ),
                                      Text('已支付')
                                    ],
                                  ),
                                  onTap: () {
                                    Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            //导航打开新视图
                                            builder: (context) =>
                                                JYZOrderPage() //打开第二个页面
                                            ));
                                  },
                                )),
                            Expanded(
                                flex: 1,
                                child: GestureDetector(
                                  child: Column(
                                    children: <Widget>[
                                      Image(
                                        image: AssetImage(
                                            'images/user_icon_order_rufund.png'),
                                        width: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                        height: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                      ),
                                      Text('退款')
                                    ],
                                  ),
                                  onTap: () {
                                    Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            //导航打开新视图
                                            builder: (context) =>
                                                JYZOrderPage() //打开第二个页面
                                            ));
                                  },
                                ))
                          ],
                        ))
                  ],
                )),
            // Card(
            //   elevation: kMargin10,
            //   child: Column(
            //     mainAxisAlignment: MainAxisAlignment.center,
            //     children: <Widget>[
            //       Row(
            //         children: <Widget>[
            //           Expanded(
            //             flex: 4,
            //             child: Container(
            //                 alignment: Alignment.centerRight,
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, kMargin10),
            //                 child: Row(
            //                   children: <Widget>[
            //                     Image(
            //                       image:
            //                           AssetImage('images/user_icon_prefer.png'),
            //                     ),
            //                     Text(
            //                       '加油偏好',
            //                       style: TextStyle(fontSize: 16),
            //                     ),
            //                   ],
            //                 )),
            //           ),
            //           Expanded(
            //               flex: 1,
            //               child: Container(
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, 0),
            //                 child: Text('修改偏好'),
            //               )),
            //         ],
            //       ),
            //       Container(
            //           margin: EdgeInsets.fromLTRB(0, kMargin28, 0, 0),
            //           child: Row(
            //             children: <Widget>[
            //               Expanded(
            //                 flex: 1,
            //                 child: Column(
            //                   children: <Widget>[
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(fontSize: 20),
            //                     )),
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(
            //                           fontSize: 15, color: Colors.grey),
            //                     )),
            //                   ],
            //                 ),
            //               ),
            //               Expanded(
            //                 flex: 1,
            //                 child: Column(
            //                   children: <Widget>[
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(fontSize: 20),
            //                     )),
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(
            //                           fontSize: 15, color: Colors.grey),
            //                     )),
            //                   ],
            //                 ),
            //               ),
            //               Expanded(
            //                 flex: 1,
            //                 child: Column(
            //                   children: <Widget>[
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(fontSize: 20),
            //                     )),
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(
            //                           fontSize: 15, color: Colors.grey),
            //                     )),
            //                   ],
            //                 ),
            //               )
            //             ],
            //           )),
            //       Row(
            //         children: <Widget>[
            //           Expanded(
            //             flex: 4,
            //             child: Container(
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, kMargin10),
            //                 child: Row(
            //                   children: <Widget>[
            //                     Image(
            //                       image: AssetImage(
            //                           'images/user_icon_authentuser_ication.png'),
            //                     ),
            //                     Text(
            //                       '车辆认证',
            //                       style: TextStyle(fontSize: 16),
            //                     ),
            //                   ],
            //                 )),
            //           ),
            //           Expanded(
            //               flex: 3,
            //               child: Container(
            //                 alignment: Alignment.centerRight,
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, 0),
            //                 child: Text('提交资料，完成认证'),
            //               )),
            //         ],
            //       ),
            //       Row(
            //         children: <Widget>[
            //           Expanded(
            //             flex: 1,
            //             child: Container(
            //               alignment: Alignment.center,
            //               margin: EdgeInsets.fromLTRB(0, 0, 0, kMargin20 * 2),
            //               child: Text('【未认证】', style: TextStyle(fontSize: 20)),
            //             ),
            //           )
            //         ],
            //       )
            //     ],
            //   ),
            // ),
            _buildTools(),
          ],
        ));
  }

  //充电站
  _buildChangeCDZ() {
    return Container(
        margin: EdgeInsets.fromLTRB(
            ScreenUtil.getInstance().setWidth(15),
            ScreenUtil.getInstance().setWidth(20),
            ScreenUtil.getInstance().setWidth(15),
            0),
        child: Column(
          children: <Widget>[
            Card(
                elevation: ScreenUtil.getInstance().setWidth(5),
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          flex: 5,
                          child: Container(
                            margin: EdgeInsets.fromLTRB(
                                ScreenUtil.getInstance().setWidth(20),
                                ScreenUtil.getInstance().setWidth(28),
                                ScreenUtil.getInstance().setWidth(10),
                                ScreenUtil.getInstance().setWidth(10)),
                            child: Text(
                              '充电订单',
                              style: TextStyle(fontSize: 16),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: Icon(Icons.next_week),
                        ),
                      ],
                    ),
                    Container(
                        margin: EdgeInsets.fromLTRB(
                            0,
                            ScreenUtil.getInstance().setWidth(28),
                            0,
                            ScreenUtil.getInstance().setWidth(28)),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                                flex: 1,
                                child: GestureDetector(
                                  child: Column(
                                    children: <Widget>[
                                      Image(
                                        image: AssetImage(
                                            'images/user_icon_order_all.png'),
                                        width: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                        height: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                      ),
                                      Text('全部')
                                    ],
                                  ),
                                  onTap: () {},
                                )),
                            Expanded(
                                flex: 1,
                                child: GestureDetector(
                                  child: Column(
                                    children: <Widget>[
                                      Image(
                                        image: AssetImage(
                                            'images/user_icon_order_paid.png'),
                                        width: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                        height: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                      ),
                                      Text('已支付')
                                    ],
                                  ),
                                  onTap: () {},
                                )),
                            Expanded(
                                flex: 1,
                                child: GestureDetector(
                                  child: Column(
                                    children: <Widget>[
                                      Image(
                                        image: AssetImage(
                                            'images/user_icon_order_rufund.png'),
                                        width: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                        height: ScreenUtil.getInstance()
                                                .setWidth(28) *
                                            2,
                                      ),
                                      Text('退款')
                                    ],
                                  ),
                                  onTap: () {},
                                ))
                          ],
                        ))
                  ],
                )),
            // Card(
            //   elevation: kMargin5,
            //   child: Column(
            //     mainAxisAlignment: MainAxisAlignment.center,
            //     children: <Widget>[
            //       Row(
            //         children: <Widget>[
            //           Expanded(
            //             flex: 4,
            //             child: Container(
            //                 alignment: Alignment.centerRight,
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, kMargin10),
            //                 child: Row(
            //                   children: <Widget>[
            //                     Image(
            //                       image:
            //                           AssetImage('images/user_icon_prefer.png'),
            //                     ),
            //                     Text(
            //                       '充电偏好',
            //                       style: TextStyle(fontSize: 16),
            //                     ),
            //                   ],
            //                 )),
            //           ),
            //           Expanded(
            //               flex: 1,
            //               child: Container(
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, 0),
            //                 child: Text('修改偏好'),
            //               )),
            //         ],
            //       ),
            //       Container(
            //           margin: EdgeInsets.fromLTRB(0, kMargin28, 0, 0),
            //           child: Row(
            //             children: <Widget>[
            //               Expanded(
            //                 flex: 1,
            //                 child: Column(
            //                   children: <Widget>[
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(fontSize: 20),
            //                     )),
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(
            //                           fontSize: 15, color: Colors.grey),
            //                     )),
            //                   ],
            //                 ),
            //               ),
            //               Expanded(
            //                 flex: 1,
            //                 child: Column(
            //                   children: <Widget>[
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(fontSize: 20),
            //                     )),
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(
            //                           fontSize: 15, color: Colors.grey),
            //                     )),
            //                   ],
            //                 ),
            //               ),
            //               Expanded(
            //                 flex: 1,
            //                 child: Column(
            //                   children: <Widget>[
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(fontSize: 20),
            //                     )),
            //                     Container(
            //                         child: Text(
            //                       '11111',
            //                       style: TextStyle(
            //                           fontSize: 15, color: Colors.grey),
            //                     )),
            //                   ],
            //                 ),
            //               )
            //             ],
            //           )),
            //       Row(
            //         children: <Widget>[
            //           Expanded(
            //             flex: 4,
            //             child: Container(
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, kMargin10),
            //                 child: Row(
            //                   children: <Widget>[
            //                     Image(
            //                       image: AssetImage(
            //                           'images/user_icon_authentuser_ication.png'),
            //                     ),
            //                     Text(
            //                       '车辆认证',
            //                       style: TextStyle(fontSize: 16),
            //                     ),
            //                   ],
            //                 )),
            //           ),
            //           Expanded(
            //               flex: 3,
            //               child: Container(
            //                 alignment: Alignment.centerRight,
            //                 margin: EdgeInsets.fromLTRB(
            //                     kMargin20, kMargin28, kMargin10, 0),
            //                 child: Text('提交资料，完成认证'),
            //               )),
            //         ],
            //       ),
            //       Row(
            //         children: <Widget>[
            //           Expanded(
            //             flex: 1,
            //             child: Container(
            //               alignment: Alignment.center,
            //               margin: EdgeInsets.fromLTRB(0, 0, 0, kMargin20 * 2),
            //               child: Text('【未认证】', style: TextStyle(fontSize: 20)),
            //             ),
            //           )
            //         ],
            //       )
            //     ],
            //   ),
            // ),
            _buildTools(),
          ],
        ));
  }

//工具
  _buildTools() {
    return new Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(8), 0,
          ScreenUtil.getInstance().setWidth(8), 0),
      color: Colors.white,
      height: ScreenUtil.getInstance().setHeight(1334) * 0.4,
      padding: EdgeInsets.all(3.0),
      child: GridView.count(
        physics: const NeverScrollableScrollPhysics(),
        crossAxisCount: 4,
        padding: EdgeInsets.all(5.0),
        children: userTools.map((item) {
          return _itemBuilder(context, item);
        }).toList(),
      ),
    );
  }

  @override
  void initState() {
    _getLoginMsg();
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    APPLIC.eventBus.on<LoginEvent>().listen((event) {
      getString(TOKEN).then((value) {
        token = value;
        _getUserMsg(value);
        _statistics(value);
      });
    });
  }

  @override
  void dispose() {
    // AMapLocationClient.shutdown();
    super.dispose();
    WidgetsBinding.instance.removeObserver(this); //销毁观察者
  }

  void _getLoginMsg() async {
    getString(TOKEN).then((value) {
      token = value;
      if (token == null || token == '') {
        return;
      }
      _getUserMsg(value);
      _statistics(value);
    });
  }

  //获取用户信息
  void _getUserMsg(token) async {
    FormData data = new FormData.fromMap({'token': token});
    Response<String> response =
        await HttpUtil().post(Api.GetUserMsgUrl, data: data);
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1) {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      preferences.setString("name", userMap['data']['user_msg']['phone']);
      if (mounted) {
        setState(() {
          APPLIC.userInfoMap = userMap['data'];
          empower(userMap['data']['user_msg']['phone']);
          user_msg = userMap['data']['user_msg'];
          user_detail = userMap['data']['user_detail'];
          avatar = user_detail['avatar'];
          uid = user_msg['uid'];
        });
      }
    }
    //  else{
    //     Fluttertoast.showToast(msg: userMap['msg']);
    //     Navigator.of(context).pushNamed("login");
    //     userMap = null;
    // }
  }

  goToBuyVip() async {
    FormData data = new FormData.fromMap({'token': token});
    Response<String> response =
        await HttpUtil().post(Api.GetUserMsgUrl, data: data);
    Map userMap = json.decode(response.data);
    Navigator.of(context).pop(1);
    print(userMap);
    if (userMap['code'] == 1) {
      if (mounted) {
        setState(() {
          if (userMap['data']['user_msg']['group_id'] == '4') {
            Fluttertoast.showToast(msg: '您已经是最高级别会员了');
          } else {
            Navigator.push(
                context,
                MaterialPageRoute(
                    //导航打开新视图
                    builder: (context) => VipShopPage(
                        group_id: (int.parse(
                                    userMap['data']['user_msg']['group_id']) +
                                1)
                            .toString()) //打开第二个页面，并传值，传递值的名称是model
                    ));
          }
        });
        APPLIC.userInfoMap = userMap['data'];
        user_msg = userMap['data']['user_msg'];
        user_detail = userMap['data']['user_detail'];
        avatar = user_detail['avatar'];
        uid = user_msg['uid'];
      }
    }
  }

  empower(String phone) async {
    Map map = new Map();
    map['phone'] = phone;
    FormData data = FormData.fromMap(new Map<String, dynamic>.from(map));
    Response<String> response =
        await HttpUtil().post(Api.kGetEmpowerURL, data: data);
    Map userMap = json.decode(response.data);
  }

  void _statistics(token) async {
    FormData data = new FormData.fromMap({'token': token});
    Response<String> response = await HttpUtil()
        .post(Api.BaseUrl + '/app.php?c=User&a=statistics', data: data);
    Map userMap = json.decode(response.data);
    if (userMap['code'] == 1) {
      if (mounted) {
        setState(() {
          balance = userMap['data']['balance'].toString(); //	余额
          point = userMap['data']['point'].toString(); //	积分
          amount_economize =
              userMap['data']['amount_economize'].toString(); //	共节省多少元
          amount_today = userMap['data']['amount_today'].toString(); //	今日收益
          amount_current =
              userMap['data']['amount_current'].toString(); //	本月预估收入
          amount_last = userMap['data']['amount_last'].toString(); //	上月结算
        });
      }
    }
  }

  card() async {
    is_bind();
    // getString('is_Bind').then((value) {
    //   if (value == null||value=='N') {

    //   } else {
    //     value == 'Y'
    //         ? Navigator.of(context)
    //             .push(new MaterialPageRoute(builder: (context) {
    //             return new DrawMoneyPage(total: user_msg['balance']);
    //           }))
    //         : Navigator.of(context)
    //             .push(new MaterialPageRoute(builder: (context) {
    //             return new BindZfbPage(type:'1');
    //           }));
    //   }
    // });
  }

  is_bind() async {
    FormData data = new FormData.fromMap({'token': token});
    Response<String> response =
        await HttpUtil().post(Api.BandingAli, data: data);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    if (code == 1 && mounted) {
      saveString('is_Bind', userMap['data']['is_banding']);
      userMap['data']['is_banding'] == 'Y'
          ? Navigator.of(context)
              .push(new MaterialPageRoute(builder: (context) {
              return new DrawMoneyPage(total: user_msg['balance']);
            }))
          : Navigator.of(context)
              .push(new MaterialPageRoute(builder: (context) {
              return new BindZfbPage(type: '1');
            }));
    }
  }
}
